package com.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.IOException;

/**
 * 返回值：
 * EVAL_BODY_INCLUDE：把Body读入存在的输出流中，doStartTag()函数可用
 * EVAL_PAGE：继续处理页面，doEndTag()函数可用
 * SKIP_BODY：忽略对Body的处理，doStartTag()和doAfterBody()函数可用
 * SKIP_PAGE：忽略对余下页面的处理，doEndTag()函数可用
 * EVAL_BODY_TAG：已经废止，由EVAL_BODY_BUFFERED取代
 * EVAL_BODY_BUFFERED：申请缓冲区，由setBodyContent()函数得到的BodyContent对象来处理tag的body，如果类实现了BodyTag，那么doStartTag()可用，否则非法
 * */
public class PageTag extends BodyTagSupport {
//out.println("<script type=\"text/javascript\">alert('123');</script>");
    private String url; // 请求controller的地址
    private String zoneName; // 包括起来的刷新区域zone的name
    private Integer total; // 总条数
    private Integer pageSize; // 每页条数
    private Integer pageNum; // ，每页展示多少条

    private String PAGE_TEMPLATE = "<div class='btn' styleStr onclick='changePage(pageNum)'>pageNum</div>";

    public int doStartTag(){
        JspWriter out = pageContext.getOut();
        try {
            out.print("div></div>"+getHtml());
            out.print(getJs());
            out.print(getStyle());
        } catch (IOException e) {
            e.printStackTrace();
        }

        return EVAL_BODY_INCLUDE;
    }

    public int doEndTag(){
        JspWriter out = pageContext.getOut();
        try {
            out.print("<div></div");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return EVAL_PAGE;
    }


    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getZoneName() {
        return zoneName;
    }

    public void setZoneName(String zoneName) {
        this.zoneName = zoneName;
    }

    public Integer getTotal() {
        return total;
    }

    public void setTotal(Integer total) {
        this.total = total;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getPageNum() {
        return pageNum;
    }

    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum;
    }

    // 拼界面
    public String getHtml(){
        StringBuffer html = new StringBuffer();
        if (this.total < 1 || this.pageNum < 1 || this.pageSize < 1) {
            return html.toString();
        }
        // 获取所有页码数量
        int tabNum = this.total / this.pageSize;
        if (this.pageSize * tabNum < this.total) {
            tabNum += 1;
        }
        for(int i = 1; i <= tabNum; i ++){
            if(pageNum == i){
                html.append(this.PAGE_TEMPLATE.replaceAll("pageNum", i+"")
                        .replace("styleStr", "style='background: blue;color:white;'"));
            }else{
                html.append(this.PAGE_TEMPLATE.replaceAll("pageNum", i+"")
                    .replace("styleStr", ""));
            }

        }
        return "<div style='display:flex;'>"+html+"</div>";
    }

    public String getJs(){
        String js = "<script type='text/javascript'>contentStr</script>";
        String changePage = "function changePage(i){ajaxAnywhere.getAJAX('"+this.url
                +"?pageSize="+pageSize+"&pageNum='+i+'"
                +"', '"+this.zoneName+"');}";
        String eval = "window[changePage]=eval(changePage);";
        return js.replace("contentStr", changePage+eval);
    }

    public String getStyle(){
        return "<style>.btn{" +
                "    height: 30px;margin-right:5px;cursor: pointer;" +
                "    width: 30px;\n" +
                "    border: 1px #a1a1a1 solid;\n" +
                "    display: flex;\n" +
                "    justify-content: center;\n" +
                "    align-items: center;\n" +
                "    border-radius: 5px;\n" +
                "    background: buttonface;" +
                "}</style>";
    }
}
